<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Http\Model\Role_per;
use App\Http\Model\Permission;
class PermissionController extends Controller
{
    /**
     * 授权列表页
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
        $data = Permission::all();
        return view('admin.permission.index',compact('data'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create(Request $request)
    {
        //
        $data = $request->except('_token');
        $res = Permission::create($data);
        if ($res) {
            return redirect('admin/per');
        }else{
            return back()->with('errors','添加失败,请重新添加');
        }
    }

    /**
     * 添加角色权限操作
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
        \DB::table('sad_permission_role')->where('rid',$request->rid)->delete();
        if(is_array($request->pid)){
            foreach ($request->pid as $key => $value) {
                \DB::table('sad_permission_role')->insert(['rid'=>$request->rid,'pid'=>$value]);
            }
        }else{
            \DB::table('sad_permission_role')->insert(['rid'=>$request->rid,'pid'=>$request->pid]);
        }
        return back()->with('errors','操作成功');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
        return view('admin.permission.create');
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
        $data = Permission::find($id);
        return view('admin.permission.edit',compact('data'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
        $data = $request->except('_token','_method');
        $per = Permission::find($id);
        $per -> name = $data['name'];
        $per -> description = $data['description'];
        $res = $per->save();
        if ($res) {
           return redirect('admin/per');
        }else{
            return back()->with('errors','操作失败');
        }

    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
        $role = Permission::find($id);
        $res = $role -> delete();
         if ($res) {
            $data = [
                'status' => 0,
                'msg' => '删除成功'
            ];
        }else{
             $data = [
                'status' => 1,
                'msg' => '删除失败'
            ];
        }
        return $data;
    }
}
